package com.example.wyz.classplus.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.example.wyz.classplus.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 10153 on 2017/5/21.
 */

public abstract class BaseActivity <P extends BasePresenter> extends AppCompatActivity{

    @BindView(R.id.mToolBar) protected Toolbar mToolBar;
    @BindView(R.id.mCoorLayout) protected CoordinatorLayout mCoorLayout;
    @BindView(R.id.mAppLayout) protected AppBarLayout mAppBarLayout;

    protected P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        initPresenter();
        mPresenter.onCreate();
        initToolBar();
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mToolBar.setTitle("");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    protected abstract int getLayout();

    protected abstract void initPresenter();

    protected final void initToolBar(){
        if(mToolBar == null){
            throw new NullPointerException("please add a toolbar in your layout");
        }else {
            setSupportActionBar(mToolBar);
        }
    }

    protected void setHomeBtnEnable(boolean isEnable){
        getSupportActionBar().setDisplayShowHomeEnabled(isEnable);
        getSupportActionBar().setDisplayHomeAsUpEnabled(isEnable);
    }

    private void checkPresenterIsNull(){
        if(mPresenter == null){
            throw new IllegalStateException("please init mPresenter in initPresenter() method ");
        }
    }



    
}
